<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class category extends Admin_Controller {
	var $model = "mcategory";
	var $view = "category";
	var $resource_admin = 'RESOURCE/static/';
    function __construct() {
        parent::__construct();
        $this->load->helper('util_helper');
        $this->load->model($this->model);
        $this->load->model('mpostcategory');
        
        $this->url_index = admin_url().$this->view;
        $this->url_edit = $this->url_index.'/edit/';
        $this->url_delete = $this->url_index.'/delete/';
    }

    public function index($page = 0) {
        $data = array();
		$this->db->order_by("cat_order asc");
        $data['datas'] = $this->{$this->model}->get_cat_tree_array();
        $this->template_data['main'] = 'admin/category/index';
		$this->template->load('layout/admin/layout',$this->template_data,$data);
    }

    
    function edit($editID = 0) {
        $data = array();

        if($editID > 0){
        	$data['edit'] = $this->{$this->model}->get($editID);
        	if(!$data['edit']){
        		redirect($this->url_edit.'0');
        	}
        }
        $post = $this->input->post();
        if ($post) {
			$post['edit']['cat_code'] = Replace_TiengViet($post['edit']['cat_name']);
			
            if ($editID > 0) {
				$post['edit']['cat_time_create'] = time();
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->{$this->model}->insert($post['edit']);
			}
            redirect($this->url_edit . $editID);
        }
		$cate_tree= array();
		$cate_tree[0] = "NONE";
		$categoryParent = $this->mcategory->gets(array('CAT_PARENT'=>0,'cat_id != '=>$editID));
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$cate_tree[$categoryParent[$i]->cat_id] = $categoryParent[$i]->cat_name;
			}
		}

		$data['cate_tree'] = $cate_tree;
        $this->template_data['main'] = 'admin/category/edit';
		$this->template->load('layout/admin/layout',$this->template_data,$data);
    }

    function delete($deleteID = 0){
		if($deleteID > 0){
			$this->{$this->model}->delete($deleteID);
			$this->{$this->model}->delete($deleteID,'cat_parent');
			$this->mpostcategory->delete($deleteID,'cat_id');
		}
	}

	/*
	
	function clist(){
		$cate_tree= array();
		$cate_sub= array();
		$categoryParent = $this->mcategory->gets(array('CAT_PARENT'=>0));
		$categorySub = $this->mcategory->gets(array('CAT_PARENT > '=>0));
		if($categorySub){
			foreach($categorySub as $sub){
				$cate_sub[$sub->cat_parent][$sub->cat_id] = '- '.$sub->cat_name;
			}
		}
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$cate_tree[$categoryParent[$i]->cat_id] = '<font color="#00f">'.$categoryParent[$i]->cat_name.'</font>';
				if(isset($cate_sub[$categoryParent[$i]->cat_id])){
					foreach($cate_sub[$categoryParent[$i]->cat_id] as $k=>$v){
						$cate_tree[$k] = $v;
					}
				}
			}
		}
		echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
    	foreach($cate_tree as $k=>$v){
    	   echo '['.$k."] ".$v."<Br>";
    	}
	}
	
	function build_link_cache(){
	    $this->load->helper('file');
	    $this->load->model("mcategory");
	    $cates = $this->mcategory->gets();
	    $data_temp =array();
	    $data = "<?php \n";
	    $data.="return ";
	    if($cates) {
	        foreach($cates as $cate){
	            if($cate->cat_code != '')
	                $data_temp[] = strtolower($cate->cat_code);
	        }
	        $data_temp = var_export($data_temp, true);
	        $data.= $data_temp;
	        $data.="?>\n";
	        write_file('./mcache/category_router.php', $data);
	    }
	
	}
	*/
}
